package collect;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TestCollection {
    public static void main(String[] args) {
        //创建集合对象
        Collection<Integer> coll = new ArrayList<>();
        //添加元素
        coll.add(1);
        coll.add(2);
        coll.add(3);
        coll.add(4);
        coll.add(5);
        //迭代器遍历
        iteratorErgodic(coll);
        System.out.println("==================================");
        //增强for遍历
        pulsForErgodic(coll);
        System.out.println("==================================");
        //Lambda遍历
        lambdaErgodic(coll);

    }

    private static void lambdaErgodic(Collection<Integer> coll) {
        coll.forEach(integer -> System.out.println(integer));
    }

    private static void pulsForErgodic(Collection<Integer> coll) {

        for (Integer integer : coll) {
            System.out.println(integer);
        }
    }

    private static void iteratorErgodic(Collection<Integer> coll) {
        //获取迭代器对象
        Iterator<Integer> it = coll.iterator();
        //利用循环获取集合中的//获取迭代器对象元素,并判断当前位置是否有元素
        while (it.hasNext()) {
            //Next方法，获取元素，移动指针
            Integer integer = it.next();
            //打印元素
            System.out.println(integer);
        }
    }
}


